import { Component, OnInit } from '@angular/core';
import { PropertyType, CRUD_MODE } from '../../shared/config/app-settings';
import { ActivatedRoute, Router } from '@angular/router';
import { AgentService } from '../../agent/common-services/agent.service';
import { PropertyService } from '../service/property.service';
import * as _ from 'lodash';
import { TranslateService } from '@ngx-translate/core';
import { UserService } from '../../shared/services/user.service';
@Component({
  selector: 'app-customer-property-management',
  templateUrl: './customer-property-management.component.html',
  styleUrls: ['./customer-property-management.component.css']
})
export class CustomerPropertyManagementComponent implements OnInit {
  userId: number;
  cols: Array<any> = [];
  selectedColumns: Array<any> = [];
  records: Array<any> = [];
  constructor(private agentService: AgentService,
    private PropertyService: PropertyService,
    private userService: UserService,
    private router: Router,
    private translateService:TranslateService,
    private route: ActivatedRoute) { }

  ngOnInit() {
    this.cols = [
      { field: 'id', header: this.translateService.instant('ID') },
      { field: 'commenceDate', header: this.translateService.instant('COMMENCE DATE') },
      { field: 'BuildingAddress', header: this.translateService.instant('BUILDING ADDRESS') },
      { field: 'Owner', header: this.translateService.instant('OWNER') },
      { field: 'weeklyRent', header: this.translateService.instant('WEEKLY RENT') },
      { field: 'lot', header: this.translateService.instant('LOT#') },
      // { field: 'commission_rent', header: this.translateService.instant('Commission') },
      { field: 'term_of_lease', header: this.translateService.instant('TERM OF LEASE') }
    ];

    this.selectedColumns = _.cloneDeep(this.cols); //toggle cols
  this.selectedColumns.splice(0,1); //default hide first column

    this.route.params.subscribe(params => {
      this.userId = +params['user-id']; // (+) converts string 'id' to a number
      console.log('login userid : ' + this.userId);
      // In a real app: dispatch action to load the details here.
      this.load();
    });

  }

  //ui event
  reload(){
    this.load();
  }

  load() {
    let filter = {
      ownerId: this.userId,
      type: PropertyType.RENT
    };
    this.PropertyService.getAllProperties(filter).subscribe(
      data => {
        this.records = data;
        _.each(this.records, (item) => {
          item.BuildingAddress = item.propertyList ? item.propertyList.buildingAddress : '';
          item.Owner = item.owner ? item.owner.code : '';
        });
      },
      err => {
        console.log(err);
      }
    )
  }



  onViewRow(rowData){
    this.router.navigate(['../../property-detail/'+rowData.id], { relativeTo: this.route, queryParams: {  menuKey: 'house-detail', mode:'view'  } });
  }



}
